package com.hongxia.assetsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.domain.SysUser;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.SysUserService;
import com.hongxia.assetsys.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Hongxia
* @description 针对表【tb_sys_user】的数据库操作Service实现
* @createDate 2024-01-06 10:34:10
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
implements SysUserService {
    @Resource
    private SysUserMapper userMapper;

    @Override
    public SysUser getUserByName(String username) {
        if(username == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        SysUser user = userMapper.selectOne(queryWrapper);
        if(user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号或密码错误");
        }
        return user;
    }

    @Override
    public long userRegister(String username, String password, String checkPassword) {
        //1.校验
        if(StringUtils.isAnyBlank(username,password,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        if(username.length() < 4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户账号长度过短");

        }
        if(password.length() < 8 || checkPassword.length() < 8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户密码长度过短");
        }
        // 账户不能包含特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(username);
        if (matcher.find()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户账号包含特殊字符");
        }
        if(!password.equals(checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户密码两次不相等");
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Integer integer = userMapper.selectCount(queryWrapper);
        if(integer > 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户账号已存在");
        }
        //2.对密码加密
        String encryptPassword = new Md5Hash(password,"salt").toHex();
        //3.插入数据
        SysUser user = new SysUser();
        user.setUsername(username);
        user.setPassword(encryptPassword);
        user.setName("tester");
        boolean saveResult = this.save(user);
        if(!saveResult){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户信息保存失败");
        }
        return user.getId();
    }

    @Override
    public SysUser getSafetyUser(SysUser loginUser) {
        if(loginUser == null){
            return null;
        }
        SysUser safetyUser = new SysUser();
        safetyUser.setId(loginUser.getId());
        safetyUser.setUsername(loginUser.getUsername());
        safetyUser.setName(loginUser.getName());
        return safetyUser;
    }
}
